package org.springblade.modules.desk.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.common.constant.RoleIdConstant;
import org.springblade.common.tool.CommonUtil;
import org.springblade.core.mp.base.BaseEntity;
import org.springblade.core.secure.utils.SecureUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.support.Kv;
import org.springblade.modules.cart.entity.Cart;
import org.springblade.modules.cart.service.ICartService;
import org.springblade.modules.commodity.entity.Commodity;
import org.springblade.modules.commodity.service.ICommodityService;
import org.springblade.modules.desk.vo.LineVO;
import org.springblade.modules.store.entity.Store;
import org.springblade.modules.store.service.IStoreService;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IUserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 首页
 *
 * @author zhuangqian
 */
@ApiIgnore
@RestController
@RequestMapping("/blade-desk/dashboard")
@AllArgsConstructor
@Api(value = "首页", tags = "首页")
public class DashBoardController {

    private IUserService userService;
    private IStoreService storeService;
    private ICommodityService commodityService;
    private ICartService cartService;

    /**
     * 活跃用户
     *
     * @return
     */
    @GetMapping("/activities")
    @ApiOperation(value = "活跃用户", notes = "活跃用户")
    public R activities() {

        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> map1 = new HashMap<>(16);
        map1.put("id", "trend-1");
        map1.put("updatedAt", "2019-01-01");
        map1.put("user", Kv.init().set("name", "曲丽丽").set("avatar", "https://gw.alipayobjects.com/zos/rmsportal/ThXAXghbEsBCCSDihZxY.png"));
        map1.put("group", Kv.init().set("name", "高逼格设计天团").set("link", "http://github.com/"));
        map1.put("project", Kv.init().set("name", "六月迭代").set("link", "http://github.com/"));
        map1.put("template", "在 @{group} 新建项目 @{project}");
        list.add(map1);

        Map<String, Object> map2 = new HashMap<>(16);
        map2.put("id", "trend-2");
        map2.put("updatedAt", "2019-01-01");
        map2.put("user", Kv.init().set("name", "付小小").set("avatar", "https://gw.alipayobjects.com/zos/rmsportal/ThXAXghbEsBCCSDihZxY.png"));
        map2.put("group", Kv.init().set("name", "高逼格设计天团").set("link", "http://github.com/"));
        map2.put("project", Kv.init().set("name", "七月月迭代").set("link", "http://github.com/"));
        map2.put("template", "在  @{group} 新建项目 @{project}");
        list.add(map2);

        return R.data(list);
    }

    /**
     * 管理员界面
     * 用户数、商家数、商品数
     *
     * @return
     */
    @GetMapping("/adminIndexCount")
    @ApiOperation(value = "管理员界面", notes = "管理员界面")
    public R adminIndexCount() {
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        int countUser = userService.count(Wrappers.<User>query().lambda()
                .eq(User::getRoleId, RoleIdConstant.USER_ROLE));
        map.put("name", "用户数");
        map.put("count", countUser);
        map.put("icon", "el-icon-user-solid");
        list.add(map);
        int countStore = storeService.count(Wrappers.<Store>query().lambda()
                .eq(Store::getApproval, "1"));
        map = new HashMap<>();
        map.put("name", "商家数");
        map.put("count", countStore);
        map.put("icon", "el-icon-s-shop");
        list.add(map);
        int countCommodity = commodityService.count();
        map = new HashMap<>();
        map.put("name", "商品数");
        map.put("count", countCommodity);
        map.put("icon", "el-icon-s-goods");
        list.add(map);
        return R.data(list);
    }

    @GetMapping("/adminLine")
    @ApiOperation(value = "商品数量折线图", notes = "商品数量折线图")
    public R adminLine(String start, String end) {
        List<String> days = CommonUtil.getDays(start, end);
        List<Integer> countList = new ArrayList<>();
        for (String day : days) {
            int count = commodityService.count(Wrappers.<Commodity>query().lambda()
                    .likeRight(BaseEntity::getCreateTime, day));
            countList.add(count);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("data", countList);
        map.put("name", "商品数增加量");
        List<Map<String, Object>> series = new ArrayList<>();
        series.add(map);
        LineVO lineVO = new LineVO();
        lineVO.setCategories(days);
        lineVO.setSeries(series);
        return R.data(lineVO);
    }

    @GetMapping("/storeLine")
    @ApiOperation(value = "商店营业额折线图", notes = "商店营业额折线图")
    public R storeLine(String start, String end) {
        List<String> days = CommonUtil.getDays(start, end);
        List<Double> countList = new ArrayList<>();
        for (String day : days) {
			List<Cart> list = cartService.list(Wrappers.<Cart>query().lambda()
					.eq(Cart::getStoreId, SecureUtil.getUserId())
					.gt(BaseEntity::getStatus, 2)
					.likeRight(BaseEntity::getUpdateTime, day));
			double sum = list.stream().mapToDouble(Cart::getCommodityPrice).sum();
			countList.add(sum);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("data", countList);
        map.put("name", "营业额");
        List<Map<String, Object>> series = new ArrayList<>();
        series.add(map);
        LineVO lineVO = new LineVO();
        lineVO.setCategories(days);
        lineVO.setSeries(series);
        return R.data(lineVO);
    }
}
